package serviceGoods

import (
	"context"
	"mxshop/app/mxshop_api/api/internal/data"
	doGoods "mxshop/app/mxshop_api/api/internal/domain/do/goods"
	dtoGoods "mxshop/app/mxshop_api/api/internal/domain/dto/goods"
	srvGoods "mxshop/app/mxshop_api/api/internal/service/goods"
)

type BannerService struct {
	data data.DataFactory
}

func (bs *BannerService) BannerList(ctx context.Context) (*dtoGoods.BannerDTOList, error) {
	var ret dtoGoods.BannerDTOList

	rsp, err := bs.data.Goods().Banner().List(ctx)
	if err != nil {
		return nil, err
	}

	for _, v := range rsp.Items {
		ret.Items = append(ret.Items, &dtoGoods.BannerDTO{
			Id:    v.Id,
			Index: v.Index,
			Image: v.Image,
			Url:   v.Url,
		})
	}

	return &ret, nil
}

func (bs *BannerService) CreateBanner(ctx context.Context, req *dtoGoods.BannerDTO) (*dtoGoods.BannerDTO, error) {
	var ret dtoGoods.BannerDTO

	reqGoods := &doGoods.BannerDO{
		Index: req.Index,
		Image: req.Image,
		Url:   req.Url,
	}
	err := bs.data.Goods().Banner().Create(ctx, reqGoods)
	if err != nil {
		return nil, err
	}

	ret.Id = reqGoods.Id

	return &ret, nil
}

func (bs *BannerService) DeleteBanner(ctx context.Context, ID int64) error {
	err := bs.data.Goods().Banner().Delete(ctx, ID)
	if err != nil {
		return err
	}

	return nil
}

func (bs *BannerService) UpdateBanner(ctx context.Context, req *dtoGoods.BannerDTO) error {
	err := bs.data.Goods().Banner().Update(ctx, &doGoods.BannerDO{
		Id:    req.Id,
		Index: req.Index,
		Image: req.Image,
		Url:   req.Url,
	})
	if err != nil {
		return err
	}

	return nil
}

func newBanner(data data.DataFactory) srvGoods.BannerSrv {
	return &BannerService{data: data}
}
